﻿using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Until.Helper;

namespace MySqlSugar
{
    public class SqlSugarRepository<T> : ISqlSugarRepository<T>
    {
        private readonly IConfiguration _configuration;
        public SqlSugarRepository(IConfiguration configuration) => this._configuration = configuration;

        public SqlSugarClient db => GetInstance();


        public List<T> GetListNotSql(Expression<Func<T, bool>> expression)
        {
            List<T> list = new List<T>();
            list = db.Queryable<T>().Where("SysUserId='1'").ToList();
            return list;
        }
        public List<T> GetListBySql(string sql)
        {
            List<T> list = new List<T>();
            list = db.Ado.SqlQuery<T>(sql);
            return list;
        }


        SqlSugarClient GetInstance()
        {
            var sqlstr = ConfigurationHelper.GetValue("Environment:DBSetting:ConnectStr");
            var dBType = ConfigurationHelper.GetValue("Environment:DBSetting:Type");
            var dbtype = DbType.MySql;
            if (dBType == "SqlServer")
            {
                dbtype = DbType.SqlServer;
            }
            else if (dBType == "MySql")
            {
                dbtype = DbType.MySql;
            }
            var db = new SqlSugarClient(
                new ConnectionConfig
                {
                    ConnectionString = sqlstr,
                    DbType = dbtype,//数据库类型
                                    //IsShardSameThread = true,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                }
            );
            return db;
        }
    }
}
